﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

public partial class _35newpart_Linq_DLINQ_LINQ_to_SQL_之面向对象的添加_查询_更新和删除 : System.Web.UI.Page
{
    // 实例化一个Linq_SQLServeDataContext（DataContext）
    Linq_SQLServeDataContext LSDb = new Linq_SQLServeDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindCategory();
        }
    }

    /// <summary>
    /// 邦定对象
    /// </summary>
    void BindCategory()
    {
        // NorthwindDataContext对象的Category属性就是Category集合
        var gv = LSDb.M_AskClasses.OrderBy(p=>p.ClaId);
        gvCategory.DataSource = gv;
        gvCategory.DataBind();
    }



    /// <summary>
    /// 当选择改变时
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        var AskClass_Arr = from p in LSDb.M_AskClasses
                           where p.ClaId == int.Parse(((GridView)sender).SelectedValue.ToString())
                           select p;
        DetailsView1.DataSource = AskClass_Arr;
        DetailsView1.DataBind();
    }

    /// <summary>
    /// 下面小东东更新数据时吉发
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
    {
        ((DetailsView)sender).ChangeMode(DetailsViewMode.Edit);//改变到编辑模式
       // DetailsView1.ChangeMode(DetailsViewMode.Edit);

        DetailsView1.DataSource = ((DetailsView)sender).DataSource;
        DetailsView1.DataBind();
    }


    /// <summary>
    /// 更新事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {

       // Response.Write(e.Keys.Count);
       // Response.Write(DetailsView1.Controls.Count);
        
    }


    #region Linq添加修改删除
    /// <summary>
    /// 添加一条新记录
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        ///实例化一个 Class表的对象
        ///并设定相关属性
        M_AskClass AskClass = new M_AskClass()
        {
            ClaName = txtCategoryName.Text,
            SpellName = txtDescription.Text
        };
#if old
       LSDb.M_AskClasses.Add(AskClass);
#endif


        //如果没有从复记录
        if (LSDb.M_AskClasses.Select(p => p.ClaName == txtCategoryName.Text).Count() == 0)
        {
            LSDb.SubmitChanges(); ///////添加东东
        }
        else
        {
            Response.Write("已存在记录" + txtCategoryName.Text);
        }

        //完成 
        if (!IsPostBack)
        {
            BindCategory();//从新帮定数据
        }

    }
    /// <summary>
    /// 删除数据
    /// </summary>
    /// <param name="sender">http://www.cnblogs.com/blusehuang/archive/2007/07/08/810485.html</param>
    /// <param name="e"></param>
    protected void gvCategory_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {///Single - 根据表达式返回集合中的某一元素；不延迟
        M_AskClass m_askclass= LSDb.M_AskClasses.Single(p=>p.ClaId==(int)gvCategory.DataKeys[e.RowIndex].Value);//得到ID
        LSDb.M_AskClasses.DeleteOnSubmit(m_askclass);//删除对象
        //执行对应的SQL命像
        LSDb.SubmitChanges();
        BindCategory();//从新帮定数据
    }



    /// <summary>
    /// http://www.cnblogs.com/blusehuang/archive/2007/07/06/808529.html
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvCategory_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        ///Single - 根据表达式返回集合中的某一元素；不延迟
        M_AskClass m_askclass = LSDb.M_AskClasses.Single(p => p.ClaId == (int)gvCategory.DataKeys[e.RowIndex].Value);//得到ID
        m_askclass.ClaName = "日";
        m_askclass.SpellName = "本";
        //执行更新
        LSDb.SubmitChanges();

        BindCategory();//从新帮定数据
    }
    /// <summary>
    /// 进入编辑状态
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvCategory_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvCategory.EditIndex = e.NewEditIndex;
        BindCategory();//从新帮定数据
    }

    //放弃事件
    #endregion
    protected void gvCategory_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvCategory.EditIndex = -1;
        BindCategory();
    }
}


